#include <QCoreApplication>
#include <QFile>
#include <QDebug>
#include <QMap>

const QString fileName = "/home/luchu/serial.txt";

void Save()
{
    QFile file(fileName);
    if (!file.open(QIODevice::WriteOnly)){
        qDebug() << "Could not open " << fileName
                 <<": " << file.errorString();
        return;
    }

    QDataStream out(&file);
    out.setVersion(QDataStream::Qt_5_4);

    int number = 99;
    out << number;

    QMap<int, QString> map;
    map.insert(1, "hello");
    map.insert(2, "kitty");
    out << map;

    file.flush();
    file.close();
}


void Load()
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)){
        qDebug() << "Could not open " << fileName
                 <<": " << file.errorString();
        return;
    }

    QDataStream in(&file);

    int Number;
    in >> Number;
    qDebug() << "Number = " << Number;

    QMap<int, QString> map;
    in >> map;

    foreach (QString str, map.values()) {
        qDebug() << str;
    }

    file.close();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Save();
    Load();

    return a.exec();
}
